package br.com.projeto.dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.projeto.sakila.bean.Address;


public class AddressDao {
	public long buscarAddress(Connection connection){
		long tempoInicial = System.currentTimeMillis();
		
		try {
			PreparedStatement preparedStatement = 
					connection.prepareStatement("select address_id, last_update, address, address2, district, postal_code, phone from address");
			
			ResultSet resultSet = preparedStatement.executeQuery();
			List<Address> lista = new ArrayList<>();
			Address address;
			while(resultSet.next()){
				address = null;
				address = new Address();
				address.setId(Integer.parseInt(resultSet.getString("address_id")));
				address.setLastUpdate(resultSet.getDate("last_update"));
				address.setAddressMember(resultSet.getString("address"));
				address.setAddress2(resultSet.getString("address2"));
				address.setDistrict(resultSet.getString("district"));
				address.setPostalCode(resultSet.getString("postal_code"));
				address.setPhone(resultSet.getString("phone"));
				
				lista.add(address);
			}
			
			resultSet.close();
			preparedStatement.close();
			
		} catch (SQLException exception) {
			System.err.println("Erro JDBC" + exception);
			throw new RuntimeException(exception);
		}
		
		long tempoFinal = System.currentTimeMillis(); 
		long tempo = tempoFinal - tempoInicial;
		return tempo;
	}
}
